<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

   protected function _initSyslog()
    {

        //Logger db
        $this->bootstrap('db');
        $dbAdapter = $this->getResource('db');

        $file = date('Ymd') . 'debug.txt';
        $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/logs/' . $file);
        $debugTxt = new Zend_Log($writer);
        Zend_Registry::set('debugTxt', $debugTxt);
        
        //Logger texto
        $file = date('Ymd') . 'log.txt';
        $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/logs/' . $file);
        $loggerTxt = new Zend_Log($writer);
        Zend_Registry::set('loggerTxt', $loggerTxt);


    }


    protected function _initVista()
    {
        
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'My_View_Helper');
        $view->headTitle('Viriathus')->setSeparator(' / ');

    }


    protected function _initSession()
    {

        Zend_Session::start();
        $_SESSION['last_debug']=microtime(true);
        
        
        $name='viriathus_'.md5($_SERVER['SERVER_ADDR'].$_SERVER['SERVER_PORT']);
        $sgit_session = new Zend_Session_Namespace($name);
        
        
        if(isset($_SESSION['server']))
        {
            if( $_SESSION['server']!=$_SERVER['HTTP_HOST'])
            {
                session_destroy();
                exit('Error: No se puede cambiar de puerto');
            }
        }

        Zend_Registry::set('sgit_session', $sgit_session);
        
     }
    
    
    

/*
    protected function _initCache()
    {

        //cache
        $frontendOptions = array('lifetime' => 36000, // 10 hora
            'automatic_serialization' => true);
        $backendOptions = array('cache_dir' => APPLICATION_PATH . '/tmp/');

        // getting a Zend_Cache_Core object
        $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
        
        Zend_Registry::set('cache', $cache);
        

        return $cache;

    }
    */
    
    protected function _initCache()
    {

        $frontendOpts = array(
            'caching' => true,
            'lifetime' => 1800,
            'automatic_serialization' => true
        );
         
        $backendOpts = array(
            'servers' =>array(
                array(
                'host'   => 'localhost',
                'port'   => 11211,
                'weight' => 1
                )
            ),
            'compression' => false
        );

        // getting a Zend_Cache_Core object
        $cache = Zend_Cache::factory('Core', 'Memcached', $frontendOpts, $backendOpts);
        
        if(isset($_GET['cache'])){
            $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
            print 'cache renovada';
        }  


        Zend_Registry::set('cache', $cache);
        

        return $cache;

    }
    


    protected function _initPlugin()
    {
        
        $this->bootstrap('db');
        $db = $this->getResource('db');
        $cache = $this->getResource('Cache');
        
        Zend_Registry::set("database", $db);
        
        // within the bootstrap file
        Zend_Locale::setDefault('es_ES');

        $inicializacion = new Sgit_Controller_Action_Helper_Profiler();
        Zend_Controller_Action_HelperBroker::addHelper($inicializacion);
        

    }
    




}
